package com.oa.office.controller;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.oa.item.biz.ItemBiz;
import com.oa.item.biz.PlanBiz;
import com.oa.item.entity.Plan;
import com.oa.office.biz.ReportTableBiz;
import com.oa.office.entity.ReportTableWithBLOBs;
import com.oa.office.util.UtilHelp;

@CrossOrigin
@Controller
@RequestMapping("/report")
public class ReportTbaleController {
	@Resource
	private ReportTableBiz reportTableBiz;
	@Resource
	private UtilHelp utilHelp;
	@Resource
	private PlanBiz planBiz;
	
	@RequestMapping("day")
	public String dayReport(Model model){
		List<ReportTableWithBLOBs> rtList=reportTableBiz.selectReportTbale("日报");
		List<Plan> planList=planBiz.selectAllPlan();
		model.addAttribute("planList", planList);
		model.addAttribute("rtList", rtList);
		model.addAttribute("reportType", "日报");
		return "office/waitAdd";
	}
	
	@RequestMapping("week")
	public String weekReport(Model model){
		List<ReportTableWithBLOBs> rtList=reportTableBiz.selectReportTbale("周报");
		List<Plan> planList=planBiz.selectAllPlan();
		model.addAttribute("planList", planList);
		model.addAttribute("rtList", rtList);
		model.addAttribute("reportType", "周报");
		return "office/waitAdd";
	}
	
	@RequestMapping("month")
	public String monthReport(Model model){
		List<ReportTableWithBLOBs> rtList=reportTableBiz.selectReportTbale("月报");
		List<Plan> planList=planBiz.selectAllPlan();
		model.addAttribute("planList", planList);
		model.addAttribute("rtList", rtList);
		model.addAttribute("reportType", "月报");
		return "office/waitAdd";
	}
	@RequestMapping("/add")
	public String add(ReportTableWithBLOBs rtwb,MultipartHttpServletRequest request,Model model){
		List<MultipartFile> files=request.getFiles("file1");
		if(files.size()>0&&files!=null){
			String path=utilHelp.getPath();
			StringBuffer sb = new StringBuffer();
			StringBuffer sb1 = new StringBuffer();
			for(MultipartFile file:files){
				String fileName=UUID.randomUUID().toString();
				File dest  = new File(path+fileName);
				String trueName=file.getOriginalFilename();
				sb.append(trueName).append(",");
				sb1.append(trueName).append(fileName).append(",");
					try {
						file.transferTo(dest);}
					 catch (Exception e) {
						e.printStackTrace();
					}
			}
			rtwb.setFileName(sb.toString());
			rtwb.setSpar(sb1.toString());
			rtwb.setCreateTime(new Date());
		}
		reportTableBiz.insertSelective(rtwb);
		List<ReportTableWithBLOBs> rtList=reportTableBiz.selectReportTbale(rtwb.getReportType());
		model.addAttribute("rtList", rtList);
		model.addAttribute("reportType", rtwb.getReportType());
		return "office/waitAdd";
	}
	
	@RequestMapping("waitUpdate")
	public String waitUpdate(Model model,@RequestParam(name="id") int id){
		ReportTableWithBLOBs rt=reportTableBiz.selectReportTableById(id);
		model.addAttribute("rt", rt);
		return "office/update";
	}
	
	@RequestMapping("update")
	public String updateReportTableWithBLOBs(ReportTableWithBLOBs rtwb,Model model){
		reportTableBiz.updateReportTableWithBLOBs(rtwb);
		List<ReportTableWithBLOBs> rtList=reportTableBiz.selectReportTbale(rtwb.getReportType());
		List<Plan> planList=planBiz.selectAllPlan();
		model.addAttribute("planList", planList);
		model.addAttribute("rtList", rtList);
		model.addAttribute("reportType", rtwb.getReportType());
		return "office/waitAdd";
	}
}
